<?php

namespace app\api\controller;


class Hosptool
{
    private $pagesize = 10;

    /**
     * 医生工具添加
     *
     */
    public function add()
    {
        $access_token = trim($_REQUEST['access_token']);
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();
         $userInfo = getUserinfoForAccessToken($access_token,2,$_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }
        $ty = trim($_REQUEST['ty']);
        $desc = trim($_REQUEST['desc']);
        $contents = trim($_REQUEST['contents']);
        $useage = trim($_REQUEST['useage']);
        $pic = trim($_REQUEST['pic']);

        $data = array(
            'uid' => $userInfo['id'],
            'hosp_id' => $_hosp,
            'ty' => $ty,
            'desc' => $desc,
            'contents' => $contents,
            'useage' => $useage,
            'img' => $pic,
            'ctime' => time(),

        );

        $toolModel = new \app\admin\model\HospDoctorTool();
        $res = $toolModel->add($data);
        if ($res) {

            $return['err'] = 0;
            $return['msg'] = '操作成功';
            return json($return);
        } else {
            $return['err'] = 1;
            $return['msg'] = '操作失败';
            return json($return);
        }
    }

    /**
     * 医生工具编辑
     *
     */
    public function edit()
    {
        $access_token = trim($_REQUEST['access_token']);
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();
        $userInfo = getUserinfoForAccessToken($access_token,2,$_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }
        $id = trim($_REQUEST['id']);
        if(empty($id))
        {
            $return['err'] = 1;
            $return['msg'] = '参数错误';
            return json($return);
        }

        $toolModel = new \app\admin\model\HospDoctorTool();
        $oldToolInfo = $toolModel->getDataForWhere(['id'=>$id,'uid'=>$userInfo['id'],'is_delete'=>0]);
        if(empty($oldToolInfo)){
            $return['err'] = 1;
            $return['msg'] = '您不能操作该记录';
            return json($return);
        }
        if(empty($oldToolInfo['hosp_id']))
        {
            $return['err'] = 1;
            $return['msg'] = '您不能操作该记录';
            return json($return);
        }
        $ty = trim($_REQUEST['ty']);
        $desc = trim($_REQUEST['desc']);
        $contents = trim($_REQUEST['contents']);
        $useage = trim($_REQUEST['useage']);
        $pic = trim($_REQUEST['pic']);

        $data = array(
            'ty' => $ty,
            'desc' => $desc,
            'contents' => $contents,
            'useage' => $useage,
            'img' => $pic,
        );


        $res = $toolModel->updateDataForWhere($data,['id'=>$oldToolInfo['id']]);
        if ($res || $res==0) {

            $return['err'] = 0;
            $return['msg'] = '操作成功';
            return json($return);
        } else {
            $return['err'] = 1;
            $return['msg'] = '操作失败';
            return json($return);
        }
    }

    public function del()
    {
        $access_token = trim($_REQUEST['access_token']);
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();
         $userInfo = getUserinfoForAccessToken($access_token,2,$_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }
        $id = trim($_REQUEST['id']);
        if(empty($id))
        {
            $return['err'] = 1;
            $return['msg'] = '参数错误';
            return json($return);
        }

        $toolModel = new \app\admin\model\HospDoctorTool();
        $oldToolInfo = $toolModel->getDataForWhere(['id'=>$id]);
        if(empty($oldToolInfo)){
            $return['err'] = 1;
            $return['msg'] = '您不能操作该记录';
            return json($return);
        }
        if(empty($oldToolInfo['hosp_id']))
        {
            $return['err'] = 1;
            $return['msg'] = '您不能操作该记录';
            return json($return);
        }
        if($oldToolInfo['uid']!=$userInfo['id']){
            $return['err'] = 1;
            $return['msg'] = '您不能操作该记录';
            return json($return);
        }

        if($oldToolInfo['is_delete']==1){
            $return['err'] = 1;
            $return['msg'] = '记录已经删除';
            return json($return);
        }



        $data = array(
            'is_delete' => 1,
        );


        $res = $toolModel->updateDataForWhere($data,['id'=>$oldToolInfo['id']]);
        if ($res || $res==0) {

            $return['err'] = 0;
            $return['msg'] = '操作成功';
            return json($return);
        } else {
            $return['err'] = 1;
            $return['msg'] = '操作失败';
            return json($return);
        }
    }



    /**
     *  医生获取订单列表
     */
    public function lists()
    {
        $access_token = trim($_REQUEST['access_token']);
        $status = trim($_REQUEST['status']);
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();
         $userInfo = getUserinfoForAccessToken($access_token,2,$_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }

        $pagesize = trim(input("pagesize"));//每页条数
        $ty = trim(input("ty"));//每页条数

        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }

        if(empty($ty)){
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $w = array('is_delete' => 0,  'hosp_id' => array("in",[0,$_hosp]),'ty'=>$ty,'uid'=>array('in',[0,$userInfo['id']]));
        $orderby = "sorts desc,id desc";

        $model = new \app\admin\model\HospDoctorTool();
        $lists = $model->lists($w, $this->pagesize, '*', $orderby);


        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);
    }
}

